﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using PowerCreator.LMS.Common.Script;
using PowerCreator.LMS.Task.Entity;
using PowerCreator.LMS.Web.Task;
using System.Collections.Generic;

public partial class sysadmin_TaskManager_TaskClassManager : PowerCreator.LMS.Web.Base.BasePage
{
    TaskManag objTaskManag = new TaskManag();
    TaskClassManag objTaskClassManag = new TaskClassManag();
    TaskClassDepartmentManag objTaskClassDepartmentManag = new TaskClassDepartmentManag();
    TaskClassUserManag objTaskClassUserManag = new TaskClassUserManag();
    MessageManag objMessageManag = new MessageManag();
    MessageUserInfoManag objMessageUserInfoManag = new MessageUserInfoManag();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.Params["id"] != null && Request.Params["id"].Trim() != "")
            {
                int taskID = int.Parse(Request.Params["id"].ToString());
                ViewState["taskID"] = taskID;


            }
            InitPage();
            string search = GetSearch();
            BinderPager(search);
            BinderPG(search, 1);
        }

    }

    protected override void InitPage()
    {
        lbl_InfoTitle_ManagerNew.Text = "训练小组管理";//GetLanageValueByKey("lab_ManagerNew_InfoTitle", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lbl_ManagerNew.Text = "新闻列表";
        btnAddNew.Text = GetLanageValueByKey("btn_AddNew", PowerCreator.LMS.Language.LanagetItemType.Page);
        btnSearch.Text = GetLanageValueByKey("btn_Search", PowerCreator.LMS.Language.LanagetItemType.Page);

        chkAll.Text = GetLanageValueByKey("chkbox_ManagerQuestion_chkboxAll", PowerCreator.LMS.Language.LanagetItemType.Page);
        chkCancel.Text = GetLanageValueByKey("chkbox_ManagerQuestion_chkboxCancel", PowerCreator.LMS.Language.LanagetItemType.Page);

        btn_DelCheck.Text = GetLanageValueByKey("btnDel", PowerCreator.LMS.Language.LanagetItemType.Page);
        btnAddNew.Text = GetLanageValueByKey("btn_AddNew", PowerCreator.LMS.Language.LanagetItemType.Page);
        btn_IsCheck.Text = GetLanageValueByKey("btnAuditing", PowerCreator.LMS.Language.LanagetItemType.Page);

        base.InitPage();

        //btnAddNew.Visible = false;
        //if (IsLicensed(PowerCreator.LMS.Action.Core.ActionInfo.AddJob))
        //    btnAddNew.Visible = true;

    }
    private string GetSearch()
    {
        string search = " FKTaskID=" + ViewState["taskID"].ToString();
        if (!string.IsNullOrEmpty(txtSearch.Text))
            search = string.Format(" and TaskCsName like '%{0}%'", txtSearch.Text.Trim());
        return search;
    }

    protected void BinderPager(string search)
    {
        AspNetPager1.RecordCount = objTaskClassManag.Count(search);
        AspNetPager1.PageSize = LMSPageSize;
    }
    private void BinderPG(string search, int pageIndex)
    {
        string orderby = " TaskCsID desc";
        DataTable dt = objTaskClassManag.Query(search, orderby, LMSPageSize, pageIndex);
        rptNew.DataSource = dt;
        rptNew.DataBind();

        PreAspNetPager(AspNetPager1);
    }
    protected void rpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
        {
            LinkButton lbtn = (LinkButton)e.Item.FindControl("lnkbtn_del");
            lbtn.Attributes.Add("onClick", "javascript:return confirm('确定删除此记录？');");
            Label lblTaskCrAuditState = (Label)e.Item.FindControl("lblTaskCrAuditState");
            if (lblTaskCrAuditState.Text == "0")
            {
                lblTaskCrAuditState.Text = "false";
            }
            else
            {
                lblTaskCrAuditState.Text = "ture";
            }
        }
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        string search = GetSearch();
        BinderPager(search);
        BinderPG(search, 1);
    }
    protected void btnAddNew_Click(object sender, EventArgs e)
    {
        string url = "AddTaskClass.aspx?id=" + ViewState["taskID"].ToString();
        Script.RedirectURL(url);
    }

    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        string search = GetSearch();
        BinderPG(search, AspNetPager1.CurrentPageIndex);
    }
    protected void rptNew_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        Label lbl_msg = (Label)e.Item.FindControl("lbl_msg");

        if (e.CommandName.ToLower() == "leditor")
        {
            if (IsLicensed("NewsUpdate"))
            {
                string url = "EditTaskClass.aspx?CID=" + e.CommandArgument.ToString() + "&TID=" + ViewState["taskID"];
                Script.RedirectURL(url);
            }
            else
            {
                lbl_msg.Text = GetLanageValueByKey("msg_ManagerNew_checkmsg", PowerCreator.LMS.Language.LanagetItemType.Page);
            }
        }
        else if (e.CommandName.ToLower() == "ldel")
        {
            if (IsLicensed("NewsDel"))
            {
                if (objTaskClassManag.Remove(int.Parse(e.CommandArgument.ToString())))
                {
                    string search = GetSearch();
                    BinderPager(search);
                    BinderPG(search, AspNetPager1.CurrentPageIndex);
                }
                else
                {
                    lbl_msg.Text = GetLanageValueByKey("Msg_ManagerQuestion_WorkNo", PowerCreator.LMS.Language.LanagetItemType.Page);
                }
            }
            else
            {
                lbl_msg.Text = GetLanageValueByKey("msg_ManagerNew_checkmsg", PowerCreator.LMS.Language.LanagetItemType.Page);
            }
        }
        else if (e.CommandName.ToLower() == "ischeck")
        {
            TaskClass vo = new TaskClass();
            vo = objTaskClassManag.Load(int.Parse(e.CommandArgument.ToString()));
            if (vo.TaskCsAuditState == 0)
                vo.TaskCsAuditState = 1;
            //else
            //{
            //    vo.TaskCrAuditState = 0;

            //}
            objTaskClassManag.Update(vo);
            string search = GetSearch();
            BinderPager(search);
            BinderPG(search, AspNetPager1.CurrentPageIndex);
        }
        else if (e.CommandName.ToLower() == "laddclass")
        {
            string url = "AddTaskClassUser.aspx?ID=" + e.CommandArgument.ToString() + "&TID=" + ViewState["taskID"].ToString();
            Script.RedirectURL(url);
        }
        else if (e.CommandName.ToLower() == "addmsg")
        {
            AddMesg(int.Parse(ViewState["taskID"].ToString()));
        }


    }
    protected void chkAll_CheckedChanged(object sender, EventArgs e)
    {
        chkCancel.Checked = false;
        //全选 / 取消
        for (int i = 0; i < rptNew.Items.Count; i++)
        {
            CheckBox cbox = (CheckBox)rptNew.Items[i].FindControl("Chbox_Check");
            if (chkAll.Checked)
                cbox.Checked = true;
            else
                cbox.Checked = false;
        }
    }
    protected void chkCancel_CheckedChanged(object sender, EventArgs e)
    {
        chkAll.Checked = false;
        //反选
        for (int i = 0; i < rptNew.Items.Count; i++)
        {
            CheckBox cbox = (CheckBox)rptNew.Items[i].FindControl("Chbox_Check");
            if (cbox.Checked)
                cbox.Checked = false;
            else
                cbox.Checked = true;
        }
    }
    protected void btn_DelCheck_Click(object sender, EventArgs e)
    {
        try
        {
            for (int i = 0; i < rptNew.Items.Count; i++)
            {
                CheckBox cbox = (CheckBox)rptNew.Items[i].FindControl("Chbox_Check");
                Label lab_NewID = (Label)rptNew.Items[i].FindControl("lab_NewID");
                if (cbox.Checked)
                {
                    if (IsLicensed("NewsDel"))
                    {
                        if (objTaskClassManag.Remove(int.Parse(lab_NewID.Text)))
                        {

                        }
                        else
                        {
                            Script.Alert(this, "msg", GetLanageValueByKey("Msg_ManagerQuestion_WorkNo", PowerCreator.LMS.Language.LanagetItemType.Page));
                        }
                    }
                    else
                    {
                        Script.Alert(this, "msg", GetLanageValueByKey("msg_ManagerNew_checkmsg", PowerCreator.LMS.Language.LanagetItemType.Page));
                    }
                }
            }
            string search = GetSearch();
            BinderPager(search);
            BinderPG(search, AspNetPager1.CurrentPageIndex);
            Script.Alert(this, "msg", GetLanageValueByKey("Msg_ManagerQuestion_WorkOK", PowerCreator.LMS.Language.LanagetItemType.Page));
        }
        catch
        {
            Script.Alert(this, "msg", GetLanageValueByKey("Msg_ManagerQuestion_WorkNo", PowerCreator.LMS.Language.LanagetItemType.Page));
        }
    }
    protected void btn_IsCheck_Click(object sender, EventArgs e)
    {
        try
        {
            for (int i = 0; i < rptNew.Items.Count; i++)
            {
                CheckBox cbox = (CheckBox)rptNew.Items[i].FindControl("Chbox_Check");
                Label lab_NewID = (Label)rptNew.Items[i].FindControl("lab_NewID");
                if (cbox.Checked)
                {
                    TaskClass vo = new TaskClass();
                    vo = objTaskClassManag.Load(int.Parse(lab_NewID.Text));
                    if (vo.TaskCsAuditState == 0)
                        vo.TaskCsAuditState = 1;
                    //else
                    //{
                    //    vo.TaskCrAuditState = 0;

                    //}
                    objTaskClassManag.Update(vo);

                }
            }
            string search = GetSearch();
            BinderPager(search);
            BinderPG(search, AspNetPager1.CurrentPageIndex);
            Script.Alert(this, "msg", GetLanageValueByKey("Msg_ManagerQuestion_WorkOK", PowerCreator.LMS.Language.LanagetItemType.Page));
        }
        catch
        {
            Script.Alert(this, "msg", GetLanageValueByKey("Msg_ManagerQuestion_WorkNo", PowerCreator.LMS.Language.LanagetItemType.Page));
        }
    }
    //发送训练任务通知
    private void AddMesg(int taskID)
    {
        VOTask objVOTask = new VOTask();
        objVOTask = objTaskManag.Load(taskID);

        Message objMessage = new Message();
        objMessage.MsgTitle = "【通知】您有新的任务，任务名称：" + objVOTask.TaskName;
        objMessage.MsgContent = "";
        objMessage.MsgType = 1;
        objMessage.FKMsgCreateID = 10000;
        objMessage.MsgCreateTime = DateTime.Now;
        int megID = objMessageManag.AddNewID(objMessage);

        TaskClass objTaskClass = new TaskClass();
        string searchTaskClass=string.Format(" FKTaskID={0}", taskID.ToString());
        DataTable dtTaskClass=objTaskClassManag.Query(searchTaskClass);

        for (int i = 0; i < dtTaskClass.Rows.Count; i++)
        {
            string searchTaskClassUser = string.Format(" FKTaskCSID={0}", dtTaskClass.Rows[i]["TaskCsID"].ToString());
            DataTable dtTaskCur = objTaskClassUserManag.Query(searchTaskClassUser);
            for (int j = 0; j < dtTaskCur.Rows.Count; j++)
            {
                MessageUserInfo objMessageUserInfo = new MessageUserInfo();
                objMessageUserInfo.FKMsgUfMsgID = megID;
                objMessageUserInfo.MsgUfUserID = int.Parse(dtTaskCur.Rows[j]["FKTaskCurUserID"].ToString());
                objMessageUserInfo.MsgUfClassID = 0;
                objMessageUserInfo.MsgUfUserType = objTaskClass.TaskCsUserType;
                objMessageUserInfo.MsgUfReadState = 0;
                objMessageUserInfo.MsgUfCreateTime = DateTime.Now;
                objMessageUserInfo.FKMsgUFCreateID = 10000;
                objMessageUserInfoManag.AddNew(objMessageUserInfo);
            }

            string searchTaskClassDept = string.Format(" FKTaskCsID={0}", dtTaskClass.Rows[i]["TaskCsID"].ToString());
            DataTable dtTaskCdt = objTaskClassDepartmentManag.Query(searchTaskClassUser);
            for (int w = 0; w < dtTaskCdt.Rows.Count; w++)
            {
                MessageUserInfo objMessageUserInfo = new MessageUserInfo();
                objMessageUserInfo.FKMsgUfMsgID = megID;
                objMessageUserInfo.MsgUfUserID = 0;
                objMessageUserInfo.MsgUfClassID = int.Parse(dtTaskCdt.Rows[w]["FKTaskCDteptID"].ToString());
                objMessageUserInfo.MsgUfUserType = objTaskClass.TaskCsUserType;
                objMessageUserInfo.MsgUfReadState = 0;
                objMessageUserInfo.MsgUfCreateTime = DateTime.Now;
                objMessageUserInfo.FKMsgUFCreateID = 10000;
                objMessageUserInfoManag.AddNew(objMessageUserInfo);
            }
        }
        Script.Alert(this,"msg","通知成功"); 
    }
}
